package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.hjxh.HjxhData;
import com.ucredit.babel.common.model.third.hjxh.HjxhISPThreeRecord;
import com.weicai.caesar.CaesarUtil;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 中互金运营商三要素验证
 *
 * @author xj
 */

public class HjxhISPThreeParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 中互金运营商三要素验证
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        HjxhISPThreeRecord obj = (HjxhISPThreeRecord) model;
        HjxhData data = obj.getData();
        JSONObject info = data.getData() == null ? new JSONObject() : JSON.parseObject(data.getData());
        String name = data.getName() == null ? "" : data.getName();
        String idNo = data.getIdCardNo() == null ? "" : CaesarUtil.encode(data.getIdCardNo());
        String mobile = data.getMobile() == null ? "" : CaesarUtil.encode(data.getMobile());
        String bankNo = data.getBankNo() == null ? "" : CaesarUtil.encode(data.getBankNo());
        StringBuilder sb = getBaseInfo(obj, successful);
        sb.append(name).append(CRT001);
        sb.append(idNo).append(CRT001);
        sb.append(mobile).append(CRT001);
        sb.append(bankNo).append(CRT001);
        sb.append(data.getCvn2() == null ? "" : data.getCvn2()).append(CRT001);
        sb.append(data.getExpired() == null ? "" : data.getExpired()).append(CRT001);
        sb.append(data.getApiType() == null ? "" : data.getApiType()).append(CRT001);
        sb.append(info.getString("code") == null ? "" : info.getString("code")).append(CRT001);
        sb.append(info.getString("message") == null ? "" : info.getString("message")).append(CRT001);
        sb.append(info.getString("requestId") == null ? "" : info.getString("requestId")).append(CRT001);
        sb.append(info.getString("result") == null ? "" : info.getString("result")).append(CRT001);
        String dataInfo = data.getData();
        JSONObject jsonObj = JSONObject.parseObject(dataInfo);
        sb.append(formatValue(jsonObj.getString("operatorType"))).append(CRT001);
        StringBuilder txSb = getBaseInfo(obj);
        key.set(getKey());
        val.set((sb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
        context.write(key, val);

        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_hujin_isp_three_inc";
    }

    @Override
    public String getType() {
        return RecordType.HJXH_ISP3_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "中互金运营商三要素验证";
    }
}
